package org.example.core;

import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CharStreams;
import org.antlr.v4.runtime.CommonTokenStream;
import org.antlr.v4.runtime.tree.ParseTree;
import org.example.funcGrammar.FuncGrammarBaseVisitor;
import org.example.funcGrammar.FuncGrammarLexer;
import org.example.funcGrammar.FuncGrammarParser;

public class FuncVisitor extends FuncGrammarBaseVisitor<String> {
    UtGenHelper.FuncParseComponent funcParseComponent = new UtGenHelper.FuncParseComponent();

    public UtGenHelper.FuncParseComponent accept(String funcContent) {
        CharStream inputFile = CharStreams.fromString(funcContent);
        FuncGrammarLexer funcLexer = new FuncGrammarLexer(inputFile);
        CommonTokenStream tokens = new CommonTokenStream(funcLexer);
        FuncGrammarParser funcParser = new FuncGrammarParser(tokens);
        ParseTree tree = funcParser.prog();
        visit(tree);
        return funcParseComponent;
    }

    @Override
    public String visitProg(FuncGrammarParser.ProgContext ctx) {
        funcParseComponent.funcName = ctx.getChild(2).getText();
        return super.visitProg(ctx);
    }

    @Override
    public String visitParameters(FuncGrammarParser.ParametersContext ctx) {
        int count = 0;
        while (count < ctx.children.size()) {
            funcParseComponent.argTypes.add(ctx.getChild(count).getText());
            funcParseComponent.argNames.add(ctx.getChild(count + 1).getText());
            count += 3;
        }
        return super.visitParameters(ctx);
    }
}
